﻿using System;
using System.Collections.Generic;
using MoonSharp.Interpreter;
using UnityEngine;
using UnityEngine.UI;

namespace SizeboxUI
{
	// Token: 0x020003AB RID: 939
	public class BehaviorSettingsView : SettingsView
	{
		// Token: 0x060018CF RID: 6351 RVA: 0x0009C3D0 File Offset: 0x0009A5D0
		private void Start()
		{
			this.Initialize();
		}

		// Token: 0x060018D0 RID: 6352 RVA: 0x0009C3D8 File Offset: 0x0009A5D8
		public void Initialize()
		{
			if (this.initialized)
			{
				return;
			}
			base.GetComponentsInChildren<Button>()[0].onClick.AddListener(delegate()
			{
				this.Close();
			});
			base.GetComponentsInChildren<Button>()[1].onClick.AddListener(delegate()
			{
				this.ApplySettings();
			});
			base.AddHeader("Basic Settings");
			this.Enabled = base.AddToggle("Enable Behavior");
			base.AddHeader("AI Settings");
			this.AIEnabled = base.AddToggle("Allow AI use");
			base.AddHeader("Script Settings");
			this.skipNo = this.gridGroup.transform.childCount;
			this.bCmd = null;
			this.initialized = true;
		}

		// Token: 0x060018D1 RID: 6353 RVA: 0x0009C497 File Offset: 0x0009A697
		private void Close()
		{
			base.gameObject.SetActive(false);
			base.transform.parent.gameObject.GetComponentInChildren<Text>().text = "Behavior Manager";
		}

		// Token: 0x060018D2 RID: 6354 RVA: 0x0009C4C4 File Offset: 0x0009A6C4
		private void ApplySettings()
		{
			if (this.bCmd == null)
			{
				return;
			}
			this.bCmd.enabled = this.Enabled.isOn;
			this.bCmd.ai = (this.bCmd.CanUseAI() && this.AIEnabled.isOn);
			for (int i = 0; i < this.bCmd.settings.Count; i++)
			{
				LuaBehavior.BehaviorSetting value = this.bCmd.settings[i];
				GameObject gameObject = this.gridGroup.transform.GetChild(this.skipNo + i).gameObject;
				switch (value.type)
				{
				case LuaBehavior.BehaviorSettingType.Bool:
					value.value = DynValue.NewBoolean(gameObject.GetComponent<Toggle>().isOn);
					break;
				case LuaBehavior.BehaviorSettingType.String:
					value.value = DynValue.NewString(gameObject.GetComponentInChildren<InputField>().text);
					break;
				case LuaBehavior.BehaviorSettingType.Float:
					value.value = DynValue.NewNumber((double)gameObject.GetComponentInChildren<Slider>().value);
					break;
				case LuaBehavior.BehaviorSettingType.Array:
					value.value = DynValue.NewNumber((double)gameObject.GetComponentInChildren<Dropdown>().value);
					break;
				case LuaBehavior.BehaviorSettingType.Keybind:
				{
					string text = gameObject.GetComponentInChildren<Button>().GetComponentInChildren<Text>().text;
					if (text[0] == '\'' && text[text.Length - 1] == '\'')
					{
						value.value = DynValue.NewString(text.Substring(1, text.Length - 2));
					}
					break;
				}
				}
				this.bCmd.settings[i] = value;
			}
			IOManager.Instance.SaveSettings(this.bCmd);
			UnityEngine.Object.FindObjectOfType<BehaviorManagerView>().ReloadBehaviorList();
			this.Close();
		}

		// Token: 0x060018D3 RID: 6355 RVA: 0x0009C69C File Offset: 0x0009A89C
		private void PopulateScriptSettings()
		{
			if (this.bCmd == null)
			{
				return;
			}
			for (int i = this.skipNo; i < this.gridGroup.transform.childCount; i++)
			{
				UnityEngine.Object.Destroy(this.gridGroup.transform.GetChild(i).gameObject);
			}
			for (int j = 0; j < this.bCmd.settings.Count; j++)
			{
				LuaBehavior.BehaviorSetting behaviorSetting = this.bCmd.settings[j];
				switch (behaviorSetting.type)
				{
				case LuaBehavior.BehaviorSettingType.Bool:
				{
					Toggle toggle = base.AddToggle(behaviorSetting.uiText);
					toggle.isOn = behaviorSetting.value.Boolean;
					break;
				}
				case LuaBehavior.BehaviorSettingType.String:
				{
					InputField inputField = base.AddInput(behaviorSetting.uiText, string.Empty);
					inputField.text = behaviorSetting.value.String;
					break;
				}
				case LuaBehavior.BehaviorSettingType.Float:
				{
					Slider slider = base.AddSlider(behaviorSetting.uiText, 0f, 1f);
					slider.value = (float)behaviorSetting.value.Number;
					break;
				}
				case LuaBehavior.BehaviorSettingType.Array:
				{
					List<Dropdown.OptionData> list = new List<Dropdown.OptionData>();
					foreach (TablePair tablePair in behaviorSetting.array.Table.Pairs)
					{
						list.Add(new Dropdown.OptionData
						{
							text = tablePair.Value.ToPrintString()
						});
					}
					Dropdown dropdown = base.AddDropdown(behaviorSetting.uiText, list);
					dropdown.value = (int)behaviorSetting.value.Number;
					break;
				}
				case LuaBehavior.BehaviorSettingType.Keybind:
				{
					Button componentInChildren = base.AddKeyBind(behaviorSetting.uiText).GetComponentInChildren<Button>();
					componentInChildren.GetComponentInChildren<Text>().text = "'" + behaviorSetting.value.String + "'";
					break;
				}
				}
			}
		}

		// Token: 0x060018D4 RID: 6356 RVA: 0x0009C8C4 File Offset: 0x0009AAC4
		public bool SetBehavior(IBehavior bObject)
		{
			this.bCmd = (bObject as LuaBehavior);
			if (this.bCmd == null)
			{
				Debug.Log("bObject isn't a LuaBehavior? What?");
				return false;
			}
			if (!this.initialized)
			{
				this.Initialize();
			}
			this.PopulateScriptSettings();
			this.UpdateValues();
			return true;
		}

		// Token: 0x060018D5 RID: 6357 RVA: 0x0009C914 File Offset: 0x0009AB14
		protected override void UpdateValues()
		{
			if (this.bCmd == null)
			{
				return;
			}
			if (this.bCmd.CanUseAI())
			{
				this.AIEnabled.isOn = this.bCmd.IsAI();
				this.AIEnabled.interactable = true;
				(this.AIEnabled.graphic as Image).sprite = (this.Enabled.graphic as Image).sprite;
			}
			else
			{
				this.AIEnabled.isOn = true;
				this.AIEnabled.interactable = false;
				(this.AIEnabled.graphic as Image).sprite = Resources.Load<Sprite>("UI/BehaviorMgr/BM_DisabledCheck");
			}
			this.Enabled.isOn = this.bCmd.IsEnabled();
		}

		// Token: 0x04001AE2 RID: 6882
		private LuaBehavior bCmd;

		// Token: 0x04001AE3 RID: 6883
		private Toggle Enabled;

		// Token: 0x04001AE4 RID: 6884
		private Toggle AIEnabled;

		// Token: 0x04001AE5 RID: 6885
		private int skipNo;
	}
}
